from threading import Thread
from time import sleep

import cv2
import pytesseract
import pyautogui
import time
import datetime
import time
from fileinput import filename

from PIL import ImageGrab

def goleft():
    # 点击寻路
    pyautogui.moveTo(1740, 330, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 点击左坐标
    pyautogui.moveTo(1269, 745, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('1')
    pyautogui.press('7')
    # 点击右坐标
    pyautogui.moveTo(1302, 747, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('6')
    pyautogui.moveTo(1481, 747, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.press('esc')

def fight2():
    print("开始刷怪")
    for i in range(15):
        print("第"+str(i)+"轮")
        rightMoveAndLeftA()
        goleft()
        time.sleep(8.5)
        RightAandMove()

def fight():
    for i in range(20):
        rightMoveAndLeftA()
        for i in range(3):
            leftAndRight()


def gohome():
    #点击寻路
    pyautogui.moveTo(1740,330,0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    #点击下拉框
    pyautogui.moveTo(1515, 424, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    #拖动下拉框
    pyautogui.moveTo(1513,526,0.1)
    pyautogui.mouseDown()
    time.sleep(6)
    pyautogui.mouseUp()
    #点击江陵
    pyautogui.moveTo(1262,540,0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    #点击左坐标
    pyautogui.moveTo(1269, 745, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('2')
    pyautogui.press('0')
    #点击右坐标
    pyautogui.moveTo(1302, 747, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('6')
    #点击移动
    pyautogui.moveTo(1481, 747, 0.1)
    for i in range(20):
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        time.sleep(1)
    pyautogui.press('esc')
    pyautogui.press('D')
    time.sleep(20)

def goWork():
    print("开始前往蜀国蓄水场")
    #点击寻路
    pyautogui.moveTo(1740,330,0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    #点击下拉框
    pyautogui.moveTo(1515, 424, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    #拖动下拉框
    pyautogui.moveTo(1509,597,0.1)
    pyautogui.mouseDown()
    time.sleep(5)
    pyautogui.mouseUp()
    #点击蓄水场
    pyautogui.moveTo(1276,640,0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    #点击左坐标
    pyautogui.moveTo(1269, 745, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('3')
    pyautogui.press('3')
    #点击右坐标
    pyautogui.moveTo(1302, 747, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('8')
    #点击移动
    pyautogui.moveTo(1481, 747, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    time.sleep(30)

    #到达蓄水场
    #点击左坐标
    pyautogui.moveTo(1269, 745, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('3')
    pyautogui.press('3')
    #点击右坐标
    pyautogui.moveTo(1302, 747, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('8')
    pyautogui.moveTo(1481, 747, 0.1)
    #
    for i in range(10):
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        time.sleep(2)
    pyautogui.press('esc')
    print("到达蜀国蓄水场")

def shopping():
    pyautogui.press('S')
    time.sleep(13)
    pyautogui.press('G')
    time.sleep(0.5)
    pyautogui.press('enter')
    time.sleep(0.5)
    pyautogui.press('down')
    time.sleep(0.5)
    pyautogui.press('down')
    time.sleep(0.5)
    pyautogui.press('enter')
    time.sleep(1)
    getImg()
    if redImg():
        #移动到商品1
        time.sleep(0.5)
        pyautogui.moveTo(731, 662, 0.1)
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        pyautogui.moveTo(630, 668, 0.1)
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        pyautogui.moveTo(791, 673, 0.1)
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        #移动到商品2
        time.sleep(0.5)
        pyautogui.moveTo(729, 715, 0.1)
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        pyautogui.moveTo(623, 724, 0.1)
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        pyautogui.moveTo(775, 719, 0.1)
        pyautogui.mouseDown()
        pyautogui.mouseUp()
        #点击交易
        pyautogui.moveTo(1152, 740, 0.1)
        pyautogui.mouseDown()
        pyautogui.mouseUp()
    time.sleep(1)
    pyautogui.press('esc')

def shopping2():
    pyautogui.press('S')
    time.sleep(13)
    pyautogui.press('G')
    time.sleep(0.5)
    pyautogui.press('enter')
    time.sleep(0.5)
    pyautogui.press('down')
    time.sleep(0.5)
    pyautogui.press('down')
    time.sleep(0.5)
    pyautogui.press('enter')
    time.sleep(1)
    #移动到商品1
    time.sleep(0.5)
    pyautogui.moveTo(731, 662, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    time.sleep(0.5)
    pyautogui.moveTo(791, 673, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    #移动到商品2
    time.sleep(0.5)
    pyautogui.moveTo(731, 662, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    time.sleep(0.5)
    pyautogui.moveTo(791, 673, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.moveTo(1029, 511, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    #回卖
    time.sleep(0.5)
    pyautogui.moveTo(1066, 517, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    time.sleep(0.5)
    pyautogui.moveTo(1006, 469, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    time.sleep(0.5)
    pyautogui.moveTo(1053, 480, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    time.sleep(0.5)
    #点击交易
    pyautogui.moveTo(1152, 740, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.moveTo(940, 556, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    time.sleep(1)
    pyautogui.press('esc')
def up():
    # 点击寻路
    pyautogui.moveTo(1740, 330, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()

    #点击左坐标
    pyautogui.moveTo(1535, 743, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('3')
    pyautogui.press('9')
    #点击右坐标
    pyautogui.moveTo(1574, 741, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('6')
    #点击移动
    pyautogui.moveTo(1741, 731, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.press('esc')
    time.sleep(7)

def down():
    # 点击寻路
    pyautogui.moveTo(1740, 330, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()

    #点击左坐标
    pyautogui.moveTo(1535, 743, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('3')
    pyautogui.press('3')
    #点击右坐标
    pyautogui.moveTo(1574, 741, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    # 输入坐标
    pyautogui.press('8')
    #点击移动
    pyautogui.moveTo(1741, 731, 0.1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    pyautogui.press('esc')
    time.sleep(5)

def rightMoveAndLeftA():
    pyautogui.keyDown('right')
    time.sleep(0.5)
    pyautogui.keyUp('right')
    pyautogui.keyDown('left')
    pyautogui.keyUp('left')
    pyautogui.press("A")
    time.sleep(1.9)

def RightAandMove():
    pyautogui.keyDown('right')
    pyautogui.keyUp('right')
    pyautogui.press("A")
    time.sleep(2)
    pyautogui.keyDown('right')
    time.sleep(8)
    pyautogui.keyUp('right')


def leftAndRight():
    pyautogui.keyDown('left')
    time.sleep(3.1)
    pyautogui.keyUp('left')
    pyautogui.keyDown('right')
    time.sleep(3.1)
    pyautogui.keyUp('right')

def getImg():
    filename='NUM1.PNG'
    ImageGrab.grab(bbox=(668, 675, 729, 689)).save(filename)
    filename='NUM2.PNG'
    ImageGrab.grab(bbox=(688, 716, 729, 740)).save(filename)

def redImg():
    # 读取图片
    image = cv2.imread("NUM1.PNG")
    #将图片转换为灰度图
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # 使用Tesseract - OCR识别数字
    text1 = pytesseract.image_to_string(gray_image, config='--psm 10 -c tessedit_char_whitelist=0123456789')
    image = cv2.imread("NUM2.PNG")
    #将图片转换为灰度图
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # 使用Tesseract - OCR识别数字
    text2 = pytesseract.image_to_string(gray_image, config='--psm 10 -c tessedit_char_whitelist=0123456789')
    print("当前数量为"+text1)
    print("当前数量为" + text2)
    print(text1 == "4\n")
    if text1 == ""  or text1 == "4\n" or text2 == "":
        print("只剩1个不卖")
        return False
    else:
        print("可以卖")
        return True


def goright():
    pyautogui.keyDown('right')
    time.sleep(7.5)

    pyautogui.keyUp('right')

def change():
    pyautogui.keyDown('alt')
    pyautogui.press('tab')
    time.sleep(0.5)
    pyautogui.keyUp('alt')


def call():
    pyautogui.press('A')

def accept():
    pyautogui.moveTo(1210,752,0.4)
    pyautogui.leftClick()
    pyautogui.moveTo(909,547,0.2)
    pyautogui.leftClick()

def changeCallAccept():
    change()
    time.sleep(1)
    call()
    time.sleep(1)
    change()
    time.sleep(1)
    accept()

def start():
    pyautogui.press(' ')
    time.sleep(0.6)
    leftA()

def leftA():
    pyautogui.press('left')
    time.sleep(0.3)
    pyautogui.press('A')

def goleft1():
    pyautogui.moveTo(1437,298,0.4)
    pyautogui.leftClick()
    pyautogui.moveTo(965,549,0.4)
    pyautogui.leftClick()
    pyautogui.leftClick()
    pyautogui.press('1')
    pyautogui.press('7')
    pyautogui.moveTo(998,552,0.4)
    pyautogui.leftClick()
    pyautogui.leftClick()
    pyautogui.press('6')
    pyautogui.moveTo(1114,546,0.4)
    pyautogui.leftClick()
    pyautogui.press('esc')
    time.sleep(8)
    pyautogui.press('right')
    time.sleep(0.1)
    pyautogui.press('A')
    time.sleep(2.3)


def shuaguai():
    for i in range(13):
        print(i)
        leftA()
        goleft1()
        goright()

#
# pytesseract.pytesseract.tesseract_cmd=r'E:\CV2\tesseract.exe'
# sleep(2)
# x,y = pyautogui.position()
# print(x,y)
# shopping2()x
#
print('倒计时10秒，准备开始')
for k in range(9):
    time.sleep(1)
    print('倒计时'+str(9-k)+'秒，准备开始')
i=1
time.sleep(1)
while(True):
    print('开始第'+str(i)+'轮')
    changeCallAccept()
    time.sleep(2)
    start()
    time.sleep(1)
    shuaguai()
    time.sleep(1)
    shopping2()
    time.sleep(1)
